package com.hu.jie.day01.fragment;


import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.hu.jie.day01.R;
import com.hu.jie.day01.adapter.TestAdapter;
import com.hu.jie.day01.bean.CommentBean;
import com.hu.jie.day01.utils.OKHttpUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class TestFragment extends Fragment {

    private String mUrl;
    private Activity mActivity;
    private RecyclerView rv;
    private List<CommentBean.DataBean.CommentsBean> mDatas;
//    private TestAdapter adapter;
    private TestAdapter adapter;

    public TestFragment() {
    }
    public static TestFragment newTnstance(String url){
        TestFragment fragment = new TestFragment();
        Bundle bundle = new Bundle();
        bundle.putString("url",url);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivity = getActivity();
        if (getArguments()!=null){
            mUrl = getArguments().getString("url");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_test, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        rv = (RecyclerView) view.findViewById(R.id.recycler);
        initData();
        mDatas = new ArrayList<>();
//        adapter = new TestAdapter(mActivity,mDatas);
        adapter = new TestAdapter(mActivity,mDatas);
        Log.i("mDatas","---123---->!!!---222-->"+mDatas.size());
        rv.setAdapter(adapter);

        RecyclerView.LayoutManager manager = new LinearLayoutManager(mActivity);
        rv.setLayoutManager(manager);
    }

    private void initData() {
        OKHttpUtils.newInstance(mActivity).getAsyncMainData(mUrl, new OKHttpUtils.MyCallBack() {
            @Override
            public void onFailure(IOException e) {

            }
            @Override
            public void onResponse(String result) {
                final CommentBean bean = new Gson().fromJson(result,CommentBean.class);
                mDatas.addAll(bean.getData().getComments());
                //Log.i("mDatas","------->!!!----->"+mDatas.size());
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        adapter.notifyDataSetChanged();
                    }
                });
            }
        });
    }
    Handler mHandler = new Handler();

}
